package com.zz.common

import scala.concurrent.Future

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import cats.effect.{ExitCode, IO, IOApp}
import com.zz.aerospike.{AsConfig, AsDAO, SimpleAsClient}
import com.zz.common.cassandra.CassandraConfig
import com.zz.common.dao.CassandraDAO
import com.zz.common.model.Config
import com.zz.common.service.IdService.{ZidMappingService, Zidservice}
import com.zz.common.service.{HttpServer, IdBundleConsumer}
import com.zz.common.zid.AerospikeZidStorage

/**
  * @author huanggh
  *         19-8-14 下午5:33
  */
object Main extends IOApp {

  val logger = com.typesafe.scalalogging.Logger(getClass)

  implicit val system: ActorSystem = ActorSystem("dsp-id-mapping-test")

  implicit val mat: ActorMaterializer = ActorMaterializer()

  implicit val ctx: GlobalContext[IO] = GlobalContext.default[IO]()

  import scala.concurrent.ExecutionContext.Implicits.global

//  val aerospike = new SimpleAsClient[IO](AsConfig("aerospike.cluster.main"))
//  val dao: AsDAO[IO] = AsDAO[IO](aerospike)
//  val asStorage = new AerospikeZidStorage(
//    dao,
//    "adx_main",
//    "zidIndex",
//    "adx_main"
//  )
//  val zidService = new Zidservice[IO](asStorage)

//  {
//    import io.circe.generic.auto._
//    import io.circe.syntax._
//    logger.info("app config:\n{}", ctx.config.asJson.spaces2)
//  }

  def run(args: List[String]): IO[ExitCode] = {

//    val dao: CassandraDAO = CassandraDAO(CassandraConfig.apply(system.settings.config))
//
//    val r: Future[List[CassandraDAO.T1]] = dao.g(List(1,3,5))
//
//    r.map(_.foreach(println))
//
//    IO.never.map(_ => ExitCode.Success)

    for {
      _ <- IdMappingSimpleService.run("0.0.0.0", 10201)
      _ <- HttpServer[IO]().serve(ctx.config.http.main)
    } yield ExitCode.Success

  }
//  def run(args: List[String]): IO[ExitCode] = {
//    for {
//      _ <- IdBundleConsumer(Config(), zidService).run().start
//      _ <- ZidMappingService.run(zidService, "0.0.0.0",  4444)
  // exitCode <- HttpServerIO.serve(ctx.config.http.main)
//    } yield exitCode
//  }
}
